require('./reset.css')
require('./login.less')
require('../../common/header/index.js')
var _account_service=require('service/account-service.js')
var _util=require('../../../util/util.js')

var _login = {
    wait : 60,
    init : function (param) {
        //隐藏手机登录的表单
        $('#phoneForm').hide();
        this.loadInfo();
        this.bindEvents();
        return this;
    },
    loadInfo : function(){
        //初次加载页面时获取验证图片：
         $('#code_image').attr('src','http://localhost:8088/user/kaptcha');
    },

    // 绑定点击事件
    bindEvents : function (param) {
        $('#phoneLogIn').click(function (param) {
            $('#passwordLogIn').removeClass('current');
            $('#phoneLogIn').addClass('current');        
            $('#phoneForm').show()
            $('#passwordForm').hide()
          })
        $('#passwordLogIn').click(function (param) {
            $('#phoneLogIn').removeClass('current');
            $('#passwordLogIn').addClass('current');
            $('#phoneForm').hide()
            $('#passwordForm').show()
          })

        var _this=this;
        //点击账号密码登录
        $('#submitPassword').on('click',function (param) {
            var formData={
                username : $.trim($('#username').val()),
                password : $.trim($('#password').val()),
                code : $.trim($('#code').val())
            };
            //对表单数据进行判断
            var result=_this.checkFormData(formData);
            if(!result.status){      //此时登陆失败
                $('#usernameMsg').text(result.usernameMsg);
                $('#passwordMsg').text(result.passwordMsg);
                $('#codeMsg').text(result.codeMsg);

            }
            else{             //初步验证通过，开始向服务器发送请求
                _account_service.login(
                    formData,
                    function (res) {
                        //登陆成功后返回上一个界面
                        let redirect=_util.getURLParam('redirect');
                        if(!!redirect)      
                            window.location.href=redirect;
                        else                
                            window.location.href='../../../view/catalog/catalog-main.html';
                      },
                    function (errMsg) {
                        if(errMsg.status == 1){
                            $('#codeMsg').text('');
                            $('#passwordMsg').text(errMsg.msg);
                        }
                        else if(errMsg.status == 101){
                            $('#passwordMsg').text('');
                            $('#codeMsg').text(errMsg.msg);
                        }
                        $('#code_image').attr('src','http://localhost:8088/user/kaptcha')
                      }
                ) 
                
            }
            return false
          })

        //点击获取手机验证码：
        $('#sendBtn').on('click',function (param) {
            var data={
                phoneNumber:$.trim($('#phonenumber').val())
            }
            if(!_util.checkValue('require',data.phoneNumber)){
                $('#phoneMsg').text('手机号不能为空');
            }
            //开始请求验证码
            else{  
                $('#phoneMsg').text(' ');
                _account_service.getPhoneCode(
                    data,
                    function(res){
                        // 成功获取验证码
                        _this.timer();
                    },
                    function(errMsg){
                        $('#phoneCodeMsg').text(errMsg.msg)
                    }
                )
            }
            
        })

        //点击手机登录
        $('#submitPhone').on('click',function (param) {
            var formData={
                phoneNumber : $.trim($('#phonenumber').val()),
                phoneCode : $.trim($('#phonecode').val()),
            };
            //对表单数据进行判断
            var result=_this.checkPhoneFormData(formData);
            //此时初次验证未通过
            if(!result.status){      
                $('#phoneMsg').text(result.phoneMsg);
                $('#phoneCodeMsg').text(result.phoneCodeMsg);
            }
            //初步验证通过，开始向服务器发送请求
            else{    
                _account_service.loginByPhone(
                    formData,
                    function (res) {
                        //登陆成功后返回上一个界面
                        let redirect=_util.getURLParam('redirect');
                        if(!!redirect)      
                            window.location.href=redirect;
                        else                
                            window.location.href='../../../view/catalog/catalog-main.html';
                      },
                    function (errMsg) {
                      if(errMsg.status == 1){
                        $('#phoneMsg').text(errMsg.msg);
                      }
                      else if(errMsg.status == 101){
                        $('#phoneCodeMsg').text(errMsg.msg);
                      }
                    }
                ) 
            }
            return false
          })

        $("#code_image").on('click',function () {
          $('#code_image').attr('src','http://localhost:8088/user/kaptcha')+new Date()
        });

    },

    //检验密码登录表单
    checkFormData:function(formData){
        var result={
            status: false,
            usernameMsg: ' ',
            passwordMsg: ' ',
            codeMsg: ' '
        };
        if(!_util.checkValue('require',formData.username)){
            result.usernameMsg='账号不能为空'
            return result
        }
        else if(!_util.checkValue('require',formData.password)){
            result.passwordMsg='密码不能为空'
            return result
        }
        else if(!_util.checkValue('require',formData.code)){
            result.codeMsg='验证码不能为空'
            return result
        }
        //验证通过
        result.status=true
        return result
    },

    // 检验手机登录表单：
    checkPhoneFormData:function (formData) {
        var result={
            status:false,
            phoneMsg:' ',
            phoneCodeMsg: ' '
        };
        if(!_util.checkValue('require',formData.phoneNumber)){
            result.phoneMsg='手机号不能为空'
            return result
        }
        else if(!_util.checkValue('require',formData.phoneCode)){
            result.phoneCodeMsg='验证码不能为空'
            return result
        }
        //初步通过验证
        result.status=true;
        return result;
      },

      //点击获取验证码之后的倒计时
      timer: function() {
        if(this.wait == 0){
            $("#sendBtn").val("获取验证码");
            $("#sendBtn").removeAttr("disabled");
            $("#sendBtn").css("border-color","1e9fff").css("background", "#ffffff").css("cursor", "pointer");
            this.wait = 60;
        }else{
            $("#sendBtn").attr("disabled","true");
            $("#sendBtn").css("border-color","fbfbfb").css("background", "#ccc").css("cursor", "not-allowed");
            $("#sendBtn").val(this.wait + "秒后重发");
            this.wait--;
            var _this = this;
            setTimeout(function() {_this.timer()}, 1000);
        }
    }

}

module.exports=_login.init();